<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
<!--          <el-form-item label="场地ID" prop="positionId">-->
<!--            <el-input v-model="queryParams.positionId" placeholder="请输入场地ID" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="管理员ID" prop="managerId">-->
<!--            <el-input v-model="queryParams.managerId" placeholder="请输入管理员ID" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
          <el-form-item label="设备场地" prop="positionId">
            <el-select v-model="queryParams.positionId" placeholder="请选择" @keyup.enter="handleQuery" clearable="true">
              <el-option
                v-for="item in positionList"
                :key="item.positionId"
                :label="item.name"
                :value="item.positionId"
                :disabled="item.status == 'discard'"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="订单编号" prop="orderNo">
            <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
<!--          <el-form-item label="订单价格" prop="amount">-->
<!--            <el-input v-model="queryParams.amount" placeholder="请输入订单价格" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="价格" prop="dividendAmount">-->
<!--            <el-input v-model="queryParams.dividendAmount" placeholder="请输入价格" clearable style="width: 240px" @keyup.enter="handleQuery" />-->
<!--          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['device:dividendRecord:add']">新增</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['device:dividendRecord:edit']">修改</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['device:dividendRecord:remove']">删除</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['device:dividendRecord:export']">导出</el-button>-->
<!--          </el-col>-->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="dividendRecordList" border="true">
        <el-table-column type="index" width="55" align="center" label="#" fixed="left"/>
        <el-table-column label="分润记录" align="center" prop="dividendId" v-if="false" />
        <el-table-column label="场地ID" align="center" prop="positionId" v-if="false"/>
        <el-table-column label="场地名称" align="center" prop="positionName"/>

        <el-table-column label="管理员ID" align="center" prop="managerId" v-if="false"/>

        <el-table-column label="订单号" align="center" prop="orderNo" />
        <el-table-column label="存柜金额" align="center" prop="amount" />
        <el-table-column label="代理商名称" align="center" prop="parentManagerUserName" :formatter="parentManagerFormatter"/>
        <el-table-column label="代理商分成" align="center" prop="dividendParentAmount" />
        <el-table-column label="场地管理员" align="center" prop="managerUserName" :formatter="managerFormatter"/>
        <el-table-column label="管理员分成" align="center" prop="dividendManagerAmount" />
        <el-table-column label="平台分成" align="center" prop="dividendPlatformAmount" />
        <el-table-column label="备注" align="center" prop="remark" />
<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--          <template #default="scope">-->
<!--            <el-tooltip content="修改" placement="top">-->
<!--              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['device:dividendRecord:edit']"></el-button>-->
<!--            </el-tooltip>-->
<!--            <el-tooltip content="删除" placement="top">-->
<!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['device:dividendRecord:remove']"></el-button>-->
<!--            </el-tooltip>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改管理员分润记录对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="dividendRecordFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="场地ID" prop="positionId">
          <el-input v-model="form.positionId" placeholder="请输入场地ID" />
        </el-form-item>
        <el-form-item label="管理员ID" prop="managerId">
          <el-input v-model="form.managerId" placeholder="请输入管理员ID" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="订单价格" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入订单价格" />
        </el-form-item>
        <el-form-item label="价格" prop="dividendAmount">
          <el-input v-model="form.dividendAmount" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DividendRecord" lang="ts">
import { listDividendRecord, getDividendRecord, delDividendRecord, addDividendRecord, updateDividendRecord } from '@/api/device/dividendRecord';
import { DividendRecordVO, DividendRecordQuery, DividendRecordForm } from '@/api/device/dividendRecord/types';
import {allPosition} from "@/api/device/position";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const dividendRecordList = ref<DividendRecordVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const dividendRecordFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: DividendRecordForm = {
  dividendId: undefined,
  positionId: undefined,
  managerId: undefined,
  orderNo: undefined,
  amount: undefined,
  dividendAmount: undefined,
  remark: undefined
}
const data = reactive<PageData<DividendRecordForm, DividendRecordQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 'create_time',
    isAsc: 'desc',
    positionId: undefined,
    managerId: undefined,
    orderNo: undefined,
    amount: undefined,
    dividendAmount: undefined,
    params: {
    }
  },
  positionList:[],
  rules: {
    dividendId: [
      { required: true, message: "分润记录不能为空", trigger: "blur" }
    ],
    positionId: [
      { required: true, message: "场地ID不能为空", trigger: "blur" }
    ],
    managerId: [
      { required: true, message: "管理员ID不能为空", trigger: "blur" }
    ],
    orderNo: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "订单价格不能为空", trigger: "blur" }
    ],
    dividendAmount: [
      { required: true, message: "价格不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules,positionList } = toRefs(data);

function managerFormatter(row){
  return row.managerUserName ? row.managerUserName : '/';
}

function parentManagerFormatter(row){
  return row.parentManagerUserName ? row.parentManagerUserName : '/';
}

const getPositionList = async () => {
  loading.value = true;
  const res = await allPosition();
  positionList.value = res.data;
  loading.value = false;
}

/** 查询管理员分润记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDividendRecord(queryParams.value);
  dividendRecordList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  dividendRecordFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: DividendRecordVO[]) => {
  ids.value = selection.map(item => item.dividendId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加管理员分润记录";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: DividendRecordVO) => {
  reset();
  const _dividendId = row?.dividendId || ids.value[0]
  const res = await getDividendRecord(_dividendId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改管理员分润记录";
}

/** 提交按钮 */
const submitForm = () => {
  dividendRecordFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.dividendId) {
        await updateDividendRecord(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addDividendRecord(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: DividendRecordVO) => {
  const _dividendIds = row?.dividendId || ids.value;
  await proxy?.$modal.confirm('是否确认删除管理员分润记录编号为"' + _dividendIds + '"的数据项？').finally(() => loading.value = false);
  await delDividendRecord(_dividendIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('device/dividendRecord/export', {
    ...queryParams.value
  }, `dividendRecord_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
  getPositionList();
});
</script>
